<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Input;
use app\admin\model\Users;
use think\Lang;
use think\Cookie;
use app\admin\model\Areas;
use app\admin\model\Networks;
class Index extends Common
{
    public function _initialize(){
        parent::_initialize();
    }
    public function index()
    {
        $networks = new Networks;
        $networks->setOutTimeNetwork();
        $map['status'] =array('in',[2,8,10]);
        $map['status'] =array('in',[2,8,10]);
        $map['status'] =array('in',[2,8,10]);
        $map['is_delete'] = 0;
        if(Users::isCustomer($this->User_id)){
            $map['user_id'] = $this->User_id;
            $networks = db('networks')->where($map)->field('name,broadband,id,user_id,type,pop_id,city_id,ecity_id')->select();
        }else if(!Users::isAdmin($this->User_id) && !Users::isService($this->User_id) && !Users::isEngineer($this->User_id)){
           $map['u.sup_id'] = $this->User_id;
           $supIds = Users::getCustomerList($this->User_id); //获取到签到管理员
            array_push($supIds, $this->User_id);
            $map['u.sup_id'] = array('in',$supIds);
            
           $map['m.status'] = array('in',[2,8,10]);
           $map['m.is_delete'] = 0;
           $networks = db('networks')->alias('m')
                    ->join("users u",'u.admin_id=m.user_id')
                    ->where($map)->field('m.name,m.id,m.user_id,m.broadband,m.type,m.pop_id,m.city_id,m.ecity_id')->select();
        }else{
            $networks = db('networks')->where($map)->field('name,id,broadband,user_id,type,pop_id,city_id,ecity_id')->select();
        }

        $lant = $lants = [];
        $data = [];
        foreach($networks as $vo){
            $star = Areas::get($vo['city_id']);
            if(intval($star['lng']) == 0 || intval($star['lat']) == 0){
                continue;
            }
            $key = $star['lat']."_".$star['lng'];
            $locat = array('name'=>$star['name'],'latitude'=>$star['lat'],'longitude'=>$star['lng']);

            array_push($lant, $key);
            array_push($lants, $key);
            $d = Areas::get($vo['ecity_id']);
            $k = $d['lat']."_".$d['lng'];
            array_push($lants, $k);
            $endcloud = array('name'=>$d['name'],'latitude'=>$d['lat'],'longitude'=>$d['lng'],'title'=>"网络: ".$vo['name']."<br>宽带: ".$vo['broadband']." MB");
            $locat['items'][] =  $endcloud;
            $locat['title'] .= "线路:".$vo['name'];
            $data[] = $locat;
        }
        /*基础的pop点*/
        // $pops = db('clouds')->select();
        // if($pops){
        //     foreach($pops as $po){
        //         $k = $po['x']."_".$po['y'];
        //         if(in_array($k, $lants) || $po['x'] == 0 || $po['y'] == 0){
        //             continue;
        //         }
        //         $locat=array('name'=>$po['name'],'latitude'=>$po['x'],'longitude'=>$po['y']);
        //         $data[] = $locat;
        //     }
        // }
        $this->assign('data',json_encode($data));
        $this->main();
        
        if(!Users::isCustomer($this->User_id)){ //管理员
           
            return $this->fetch('main');
        }else{
            return $this->fetch();
        }
        
    }
    public function main(){
        $version = Db::query('SELECT VERSION() AS ver');
        $config  = [
            'url'             => $_SERVER['HTTP_HOST'],
            'document_root'   => $_SERVER['DOCUMENT_ROOT'],
            'server_os'       => PHP_OS,
            'server_port'     => $_SERVER['SERVER_PORT'],
            'server_ip'       => $_SERVER['SERVER_ADDR'],
            'server_soft'     => $_SERVER['SERVER_SOFTWARE'],
            'php_version'     => PHP_VERSION,
            'mysql_version'   => $version[0]['ver'],
            'max_upload_size' => ini_get('upload_max_filesize')
        ];
        $mso['m.status']=array('in',[2,8,10]);
        $mso['m.is_delete'] = 0;
        $mso['m.enddate'] = array('egt',strtotime(date('Y-m-d',time())));
        $map['group_id'] = config('customer_id');
        
        if(Users::isAdmin($this->User_id) || Users::isEngineer($this->User_id)){
            $maps['touser'] = $this->User_id;
            $to['network'] = db('networks')->alias('m')->where($mso)->count();
            $to['outtime'] = db('networks')->alias('m')->where($mso)->where('m.enddate',"lt",strtotime("+1 month", time()))->count();
        }elseif(Users::isService($this->User_id)){
            $maps['touser'] = $this->User_id;
            $to['network'] = db('networks')->alias('m')->where($mso)->count();
            $to['outtime'] = db('networks')->alias('m')->where($mso)->where('m.enddate',"lt",strtotime("+1 month", time()))->count();
        }elseif(Users::isCustomer($this->User_id)){
            $maps['touser'] = $this->User_id;
            $mso['m.user_id'] = $this->User_id;
            $to['network'] = db('networks')->alias('m')->where($mso)->count();
            $to['outtime'] = db('networks')->alias('m')->where($mso)->where('m.enddate',"lt",strtotime("+1 month", time()))->count();
        }else{
            $map['sup_id'] = $this->User_id;
            $maps['touser'] = $this->User_id;
            $mso['u.sup_id'] = $this->User_id;
            $to['network'] = db('networks')->alias('m')
                              ->join('users u',"u.admin_id=m.user_id")
                        ->where($mso)->count();
            $to['outtime'] = db('networks')->alias('m')->join('users u',"u.admin_id=m.user_id")->where($mso)->where('m.enddate',"lt",strtotime("+1 month", time()))->count();
        }
        $to['users'] = db('Users')->where($map)->count();
        /*未读消息*/
        $maps['is_red'] = 0;
        $to['message'] = db('Messages')->where($maps)->count();
        $this->assign('config', $config);
        $this->assign('to', $to);
    }

    public function navbar(){
        return $this->fetch();
    }
    public function nav(){
        return $this->fetch();
    }
    public function clear(){
        $R = RUNTIME_PATH;
        if ($this->_deleteDir($R)) {
            $result['info'] = lang('Scavenging caching success!');
            $result['status'] = 1;
        } else {
            $result['info'] = lang('Scavenging cache failed!');
            $result['status'] = 0;
        }
        return $result;
    }
    private function _deleteDir($R)
    {
        $handle = opendir($R);
        while (($item = readdir($handle)) !== false) {
            if ($item != '.' and $item != '..') {
                if (is_dir($R . '/' . $item)) {
                    $this->_deleteDir($R . '/' . $item);
                } else {
                    if (!unlink($R . '/' . $item))
                        die('error!');
                }
            }
        }
        closedir($handle);
        return rmdir($R);
    }
    
    
    
}
